//
//  DHHomeRecommendCollectionCell.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/4/13.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHHomeRecommendCollectionCell.h"

@interface DHHomeRecommendCollectionCell()

@property (assign, nonatomic) BOOL liked;
@property (strong, nonatomic) NSDictionary *model;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *lineViewLeftLayout;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *lineViewWidthLayout;

@end

@implementation DHHomeRecommendCollectionCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    self.stateLabel.layer.cornerRadius = self.stateLabel.height / 2;
    self.stateLabel.layer.masksToBounds = YES;
    self.stateLabel.layer.borderWidth = 2;
    self.stateLabel.layer.borderColor = [[UIColor whiteColor] CGColor];
    self.freshLabel.layer.cornerRadius = self.freshLabel.height / 2;
    self.freshLabel.layer.masksToBounds = YES;
    
    [self.likeButton bk_addEventHandler:^(id sender) {
        NSDictionary *params = @{@"product_id":[self.model objectForKey:@"_id"],
                                 @"collect":[NSNumber numberWithBool:!self.liked]
                                 };
        if (self.liked) {
            [self.likeButton setImage:[UIImage imageNamed:@"product_unlike"] forState:UIControlStateNormal];
            [[DHNetAPIClient shareDHNetAPIClient] request_product_collectWithParams:params andBlock:^(id data, NSError *error) {
                self.liked = !self.liked;
            }];
        }else{
            [self.likeButton setImage:[UIImage imageNamed:@"product_like"] forState:UIControlStateNormal];
            [[DHNetAPIClient shareDHNetAPIClient] request_product_collectWithParams:params andBlock:^(id data, NSError *error) {
                self.liked = !self.liked;
            }];
        }
    } forControlEvents:UIControlEventTouchUpInside];
}

- (void)updateCell:(NSDictionary *)model{
    self.liked = [[model objectForKey:@"collected"] boolValue];
    self.model = model;
    
    if ([[model objectForKey:@"snapshots"] count] > 0) {
        [self.imageView dh_setImageURLString:[[model objectForKey:@"snapshots"] objectAtIndex:0]];
    }else{
        [self.imageView setImage:[UIImage imageNamed:@"img_default.jpg"]];
    }
    self.nameLabel.text = [model objectForKey:@"name"];
    if (self.liked) {
        [self.likeButton setImage:[UIImage imageNamed:@"product_like"] forState:UIControlStateNormal];
    }else{
        [self.likeButton setImage:[UIImage imageNamed:@"product_unlike"] forState:UIControlStateNormal];
    }
    
    self.rightPriceLabel.text = [NSString stringWithFormat:@" ￥%@",[model objectForKey:@"sales_price"]];
    
    self.lineViewLeftLayout.constant = self.rightPriceLabel.left + 17;
    self.lineViewWidthLayout.constant = [self.rightPriceLabel.text widthForFont:self.rightPriceLabel.font] - 2;
    
    // 中划线
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:self.rightPriceLabel.text attributes:attribtDic];
    self.rightPriceLabel.attributedText = attribtStr;
    
    self.typeLabel.text = [[model objectForKey:@"brand"] objectForKey:@"name"];
    self.typeLabel.adjustsFontSizeToFitWidth = YES;
    
    CGFloat sales_price= [[model objectForKey:@"sales_price"] floatValue];
    CGFloat discount_price = [[model objectForKey:@"discount_price"] floatValue];
    if (sales_price == discount_price) {
        self.rightPriceLabel.hidden = YES;
        self.leftPriceLabel.textAlignment = NSTextAlignmentCenter;
        if ([[model objectForKey:@"can_be_purchased"] boolValue] && ![[model objectForKey:@"can_be_leased"] boolValue]) {
            self.stateLabel.text = @"售";
            self.stateLabel.backgroundColor = [UIColor colorWithHexString:@"#5F657B"];
            self.leftPriceLabel.text = [NSString stringWithFormat:@"￥%@",[model objectForKey:@"discount_price"]];
        }else{
            self.stateLabel.text = @"租";
            self.stateLabel.backgroundColor = [UIColor colorWithHexString:@"#F7BB18"];
            self.leftPriceLabel.text = [NSString stringWithFormat:@"￥%@",[model objectForKey:@"lease_price"]];
        }
    }else{
        self.leftPriceLabel.textAlignment = NSTextAlignmentRight;
        if ([[model objectForKey:@"can_be_purchased"] boolValue] && ![[model objectForKey:@"can_be_leased"] boolValue]) {
            self.stateLabel.text = @"售";
            self.stateLabel.backgroundColor = [UIColor colorWithHexString:@"#5F657B"];
            self.leftPriceLabel.text = [NSString stringWithFormat:@"￥%@ /",[model objectForKey:@"discount_price"]];
        }else{
            self.stateLabel.text = @"租";
            self.stateLabel.backgroundColor = [UIColor colorWithHexString:@"#F7BB18"];
            self.leftPriceLabel.text = [NSString stringWithFormat:@"￥%@ /",[model objectForKey:@"lease_price"]];
        }
    }
}

@end
